﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using ServEvo.Layout.Interfaces;
using ServEvo.Layout.Enum;

namespace ServEvo.ServControl
{
    class ServiceManager : MainManager, IService, IDisposable
    {
        protected List<Service> _listServices;
        protected Dictionary<Service, ServiceController> _listServiceController;

        public void Dispose()
        {
            foreach (KeyValuePair<Service, ServiceController> o in _listServiceController)
            {
                o.Value.Dispose();
            }
        }

        List<Service> getListServices()
        {
            return _listServices;
        }
        List<Service> IService.getListServices()
        {
            return _listServices;
        }

        protected void initList()
        {
            _listServiceController = new Dictionary<Service, ServiceController>();

            foreach (Service service in _listServices)
            {
                _listServiceController.Add(service, null);
            }
        }

        public bool isServiceStarted()
        {
            bool bStatut = true;
            foreach (Service service in _listServices)
            {
                bStatut &= isServiceStarted(service);
            }
            return bStatut;
        }
        public bool isServiceStarted(Service service)
        {
            bool bRetour = false;
            if (_listServices.Contains(service))
            {
                _listServiceController[service].Refresh();
                if (_listServiceController[service].Status == ServiceControllerStatus.Running) bRetour = true;
            }
            return bRetour;
        }

        public void startService()
        {
            foreach (Service service in _listServices)
            {
                startService(service);
            }
        }
        public void startService(Service service)
        {
            lock (oLock)
            {
                if (_listServices.Contains(service))
                {
                    _listServiceController[service].Refresh();
                    if (_listServiceController[service].Status == ServiceControllerStatus.Stopped)
                    {
                        _listServiceController[service].Start();
                        //scHTTP.WaitForStatus(ServiceControllerStatus.Running);
                    }
                    else
                    {
                        if (_listServiceController[service].Status == ServiceControllerStatus.StopPending)
                        {
                            _listServiceController[service].WaitForStatus(ServiceControllerStatus.Stopped);
                            _listServiceController[service].Start();
                        }
                    }
                }
            }
        }

        public void restartService()
        {
            foreach (Service service in _listServices)
            {
                restartService(service);
            }
        }
        public void restartService(Service service)
        {
            lock (oLock)
            {
                if (_listServices.Contains(service))
                {
                    _listServiceController[service].Refresh();
                    stopService(service);
                    _listServiceController[service].WaitForStatus(ServiceControllerStatus.Stopped);
                    startService(service);
                }
            }
        }

        public void stopService()
        {
            foreach (Service service in _listServices)
            {
                stopService(service);
            }
        }
        public void stopService(Service service)
        {
            lock (oLock)
            {
                if (_listServices.Contains(service))
                {
                    _listServiceController[service].Refresh();
                    if (_listServiceController[service].Status == ServiceControllerStatus.Running
                        || _listServiceController[service].Status == ServiceControllerStatus.Paused)
                    {
                        _listServiceController[service].Stop();
                    }
                    else
                    {
                        if (_listServiceController[service].Status == ServiceControllerStatus.ContinuePending)
                        {
                            _listServiceController[service].WaitForStatus(ServiceControllerStatus.Running);
                            _listServiceController[service].Stop();
                        }
                    }
                }
            }
        }
    }
}
